package pers.qianyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
import pers.qianyu.converter.*;
import pers.qianyu.domain.common.*;
import pers.qianyu.domain.dto.*;
import pers.qianyu.domain.entity.*;
import pers.qianyu.domain.vo.*;
import pers.qianyu.mapper.*;
import pers.qianyu.service.*;
import pers.qianyu.util.*;

/**
 * 用户服务实现类
 *
 * @author mizzle rain
 * @date 2020-10-12 9:32
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserConverter userConverter;

    @Override
    public int save(UserDTO userDTO) {
        UserDO userDO = userConverter.convertToDO(userDTO);
        return userMapper.insert(userDO);
    }

    @Override
    public int update(Long id, UserDTO userDTO) {
        UserDO userDO = userConverter.convertToDO(userDTO);
        userDO.setId(id);
        return userMapper.updateById(userDO);
    }

    @Override
    public int delete(Long id) {
        return userMapper.deleteById(id);
    }

    @Override
    public Pagination<UserVO> query(UserQueryDTO userQueryDTO) {
        ValidatorUtils.validate(userQueryDTO);
        // 构造查询条件
        Page<UserDO> page = new Page<>(userQueryDTO.getPageNo(), userQueryDTO.getPageSize());
        UserDO query = userConverter.convertToDO(userQueryDTO);
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>(query);
        // 查询结果
        Page<UserDO> selectPage = userMapper.selectPage(page, wrapper);
        // 结果解析
        Pagination<UserVO> pagination = userConverter.convertToPagination(selectPage);
        return pagination;
    }
}
